package org.mat.framework.lang.dto;

import org.mat.framework.lang.constant.DefaultErrors;
import org.mat.framework.lang.exception.ErrorInfo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>Title: MatApiResponse</p>
 * <p>Date: 2019/6/27 </p>
 * <p>Description: </p>
 *
 * @author sunxinhe
 */
public class MatApiResponse<T> {

    private Boolean success = true;

    private String code = "200";

    private String message;

    private String serverTime;

    private T data;

    private long timestamp = System.currentTimeMillis();

    public MatApiResponse() {

    }

    public MatApiResponse(ErrorInfo errorInfo) {
        this.setSuccess(false);
        this.setCode(errorInfo.getCode());
        this.setMessage(errorInfo.getMsg());
        this.setServerTime(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
    }


    /**
     * 为兼容ipaas代码，保留int类型的code入参，TODO 后续整理异常定义并逐步替换
     * @param code
     * @param message
     */
    @Deprecated
    public MatApiResponse(Integer code, String message) {
        this.code = String.valueOf(code);
        this.message = message;
    }

    public MatApiResponse(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public MatApiResponse(String code, String message, String serverTime, T data) {
        this.code = code;
        this.message = message;
        this.serverTime = serverTime;
        this.data = data;
    }


    public Boolean getSuccess() {
        return success;
    }

    public MatApiResponse setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public String getCode() {
        return code;
    }

    public MatApiResponse setCode(ErrorInfo errorInfo) {
        this.code = errorInfo.getCode();
        this.message = errorInfo.getMsg();
        return this;
    }

    public MatApiResponse setErrorInfo(ErrorInfo errorInfo) {
        this.code = errorInfo.getCode();
        this.message = errorInfo.getMsg();
        return this;
    }

    public MatApiResponse setCode(String code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public MatApiResponse setMessage(String message) {
        this.message = message;
        return this;
    }

    public String getServerTime() {
        return serverTime;
    }

    public MatApiResponse setServerTime(String serverTime) {
        this.serverTime = serverTime;
        return this;
    }

    public T getData() {
        return data;
    }

    public MatApiResponse setData(T data) {
        this.data = data;
        return this;
    }


    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public static <T> MatApiResponse<T> success() {
        return new MatApiResponse()
                .setCode("200")
                .setServerTime(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
                .setMessage("Success");
    }

    public static <T> MatApiResponse<T> success(T data) {
        return new MatApiResponse()
                .setCode("200")
                .setServerTime(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
                .setMessage("Success")
                .setData(data);
    }

    public static <T> MatApiResponse<T> success(String message, T data) {
        return new MatApiResponse()
                .setCode("200")
                .setServerTime(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
                .setMessage(message)
                .setData(data);
    }

    public static <T> MatApiResponse<T> fail(String message) {
        return new MatApiResponse()
                .setSuccess(false)
                .setCode(DefaultErrors.DEFAULT.getCode())
                .setServerTime(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
                .setMessage(message);
    }

    public static <T> MatApiResponse<T> fail(String code, String message) {
        return new MatApiResponse()
                .setSuccess(false)
                .setCode(code)
                .setServerTime(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
                .setMessage(message);
    }

    public static <T> MatApiResponse<T> fail(String message, T data) {
        return new MatApiResponse()
                .setSuccess(false)
                .setCode(DefaultErrors.DEFAULT.getCode())
                .setData(data)
                .setServerTime(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
                .setMessage(message);
    }


    /**
     * 为兼容ipaas代码，保留int类型的code入参，TODO 后续整理异常定义并逐步替换
     * @param code
     * @param msg
     * @return
     */
    @Deprecated
    public static <T> MatApiResponse<T> fail(Integer code, String msg) {
        MatApiResponse<T> r = new MatApiResponse<T>();
        r.setCode(String.valueOf(code));
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }


}
